package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceReportDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 * 
 * @author ghy
 * @date 2025-08-21
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;


    /**
     * 根据ProductKey查询详情
     * @param productKey
     * @return
     */
    @ApiOperation("根据ProductKey查询详情")
    @GetMapping("/queryProduct/{productKey}")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey) {
        return R.ok(deviceService.queryProduct(productKey));
    }


    /**
     * 查询设备上报数据
     *
     * @return
     */
    @ApiOperation("查询设备上报数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceReportDataVo>> queryServiceProperties(@PathVariable String iotId) {
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /**
     * 查询设备详情
     *
     * @return
     */
    @ApiOperation("查询设备详情")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getByIotId(@PathVariable String iotId) {
        return R.ok(deviceService.getByIotId(iotId));
    }

    /**
     * 注册设备
     *
     * @return
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @ApiOperation("查询所有产品列表")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct() {
        List<ProductVo> productVoList = deviceService.allProduct();
        return R.ok(productVoList);
    }


    /**
     * 同步产品列表
     */
    @ApiOperation("同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询设备列表
     */
    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<Device> list(@ApiParam("设备查询条件") Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

}
